package com.xiaolin.login.controller;

import com.xiaolin.login.model.Member;
import com.xiaolin.login.utils.TokenUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

/**
 * @ClassName ViewController
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/10/7
 * @Version 1.0
 */
@Controller
@RequestMapping("/sso/view")
public class ViewController {
    @RequestMapping("/page")
    public String loginPage(@RequestParam(required = false,value = "redirect") String redirect,
                            HttpSession session,
                            @CookieValue(required = false,value = "token") Cookie cookie){
        if(cookie!=null){
            //有cookie就直接响应回去
            Member user = (Member) TokenUtils.cache.get(cookie.getValue());
            if(user !=null){
                return "redirect:"+redirect;
            }
        }
        if(!StringUtils.isEmpty(redirect)){
            session.setAttribute("redirect",redirect);
        }
        return "login";
    }
}
